package com.sto.stoapp.adapter;

import java.util.List;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sto.stoapp.activity.R;

/**
 * 配对蓝牙适配器
 * 
 * @author mgf
 * 
 */
public class BluePairAdapter extends BaseAdapter {
	private Context context;

	private List<BluetoothDevice> devices;

	/**
	 * 构造函数
	 * 
	 * @param context
	 *            上下文
	 * @param devices
	 *            蓝牙设备
	 */
	public BluePairAdapter(Context context, List<BluetoothDevice> devices) {
		this.context = context;
		this.devices = devices;
	}

	@Override
	public int getCount() {
		return devices == null ? 0 : devices.size();
	}

	@Override
	public Object getItem(int arg0) {
		return devices.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		Holder holder;
		if (arg1 == null) {
			holder = new Holder();
			arg1 = LayoutInflater.from(context).inflate(
					R.layout.blue_devices_adapter, null);
			holder.blueName = (TextView) arg1.findViewById(R.id.blue_name);
			arg1.setTag(holder);
		} else {
			holder = (Holder) arg1.getTag();
		}
		holder.blueName.setText(devices.get(arg0).getName());
		return arg1;
	}

	class Holder {
		TextView blueName;
	}
}
